<template>
	<view class="orderDetails">	
		<view class="top">
			<view class="state">
				<view class="right">
					<text>{{info.order_status_name}}</text>
					<text class="time" v-if="info.order_status==0">下单后请30分钟内付款，超时订单将自动关闭</text>
				</view>
			    <image v-if="info.order_status==0" src="https://hdsq.aoorange.cn/attachment/xcx/icon/order_bg.png" mode="aspectFill"></image>
				<image v-if="info.order_status==1" src="https://hdsq.aoorange.cn/attachment/xcx/icon/order_bg2.png" mode="aspectFill"></image>
				<image v-if="info.order_status==2||info.order_status==3||info.order_status==5" src="https://hdsq.aoorange.cn/attachment/xcx/icon/order_bg1.png" mode="aspectFill"></image>
				<image v-if="info.order_status==4" src="https://hdsq.aoorange.cn/attachment/xcx/icon/order_ok.png" mode="aspectFill"></image>
			</view>
			<view class="common" v-if="info.shipping_time != ''">
				<text>预计送达时间</text>
				<text>{{info.shipping_time}}</text>
			</view>
			<view class="address" v-if="info.shipping_type == 1">
				<image src="https://hdsq.aoorange.cn/attachment/xcx/icon/address.png" mode="widthFix"></image>
				<view class="right">
					<view class="txt">
						<text>收货人：{{info.receiver_name}}</text>
						<text>{{info.receiver_mobile}}</text>
					</view>
					<view class="txt1">
						收货地址：{{info.receiver_address}}
					</view>
					<view class="jt"></view>
				</view>
			</view>
			<view class="address" v-if="info.shipping_type == 2">
				<image src="https://hdsq.aoorange.cn/attachment/xcx/icon/address.png" mode="widthFix"></image>
				<view class="right">
					<view class="txt1" @click="showMap()">
						自提地址：{{info.receiver_address}}
					</view>
					<view class="jt"></view>
				</view>
			</view>
		</view>
		<view class="goods" style="border-bottom: 1upx solid #ddd;" @click="jump('store_view', info.merchantId)">
			<image :src="info.merchantPicture" mode="aspectFill" style="width: 50upx;height: 50upx;"></image>
			<text>{{info.merchantName}}</text>
		</view>
		<view class="goods" v-for="(item,index) in info.product_list" :key="index" >
			<image :src="item.product_picture" mode="aspectFill" @click="jump('product_view', 'id='+item.product_id+'&merchantId='+info.merchantId)"></image>
			<view class="right">
				<text @click="jump('product_view', 'id='+item.product_id+'&merchantId='+info.merchantId)">{{item.product_name}}</text>
				<view class="size" @click="jump('product_view', 'id='+item.product_id+'&merchantId='+info.merchantId)">
					<text>{{item.sku_name}}</text>
					<text>x{{item.actual_num}}</text>
					<text>{{item.status_name}}</text>
				</view>
				<text class="num" @click="jump('product_view', 'id='+item.product_id+'&merchantId='+info.merchantId)">
					{{item.adjust_money > 0 ? '￥'+item.adjust_money : ''}}
				</text>
				<view class="operate" v-if="item.button_list.length > 0">
					<text v-for="(button,index1) in item.button_list" :key="index1" @click="jump(button.type, item.id)" class="sel">
						{{button.name}}
					</text>
				</view>
			</view>
		</view>
		<view class="line">
			<text>商品金额</text>
			<text>￥{{info.product_money - info.coin_money}}</text>
		</view>
		<view class="line" v-if="info.shipping_money > 0">
			<text>配送费</text>
			<text>+{{info.shipping_money > 0 ? '￥'+info.shipping_money : "包邮"}}</text>
		</view>
		<view class="line" v-if="info.give_coin > 0">
			<text>打包费</text>
			<text>+{{info.give_coin > 0 ? '￥'+info.give_coin : "包邮"}}</text>
		</view>
		<view class="line" v-if="info.refund_money > 0">
			<text>退款金额</text>
			<text>-{{'￥'+info.refund_money}}</text>
		</view>
		<view class="line" v-if="info.coupon_money > 0">
			<text>优惠券抵扣</text>
			<text>-{{'￥'+info.coupon_money}}</text>
		</view>
		<view class="pic">
			<text>合计：</text>
			<text>{{'￥'+info.order_money}}</text>
		</view>
		<view class="common">
			<text>买家留言</text>
			<text>{{info.buyer_message}}</text>
		</view>
		<view class="info">
			<text>订单信息：</text>
			<text>订单号码：{{info.order_sn}}</text>
			<text>下单时间：{{info.create_time}}</text>
			<text v-if="info.pay_time != ''">支付时间：{{info.pay_time}}</text>
			<text v-if="info.lobby_eat_time != ''">商家接单时间：{{info.lobby_eat_time}}</text>
			<text v-if="info.consign_time != ''">发货时间：{{info.consign_time}}</text>
			<text v-if="info.delivery_type == 1">骑手信息：{{info.delivery_company + '：' + info.delivery_no}}</text>
			<text v-if="info.delivery_type === '0'">无需物流</text>
			<text v-if="info.sign_time != ''">签收时间：{{info.sign_time}}</text>
			<text v-if="info.cancel_time != ''">取消时间：{{info.cancel_time}}</text>
		</view>
		<view class="bot" v-if="info.button_list.length > 0">
			<view v-if="info.button_list.length > 0" :class="info.button_list.length == 1 ? 'txt1' : 'txt2'">
				<text v-for="(button,index2) in info.button_list" :key="index2" @click="operate(button.type, info.id)">{{button.name}}</text>
			</view>
		</view>
		<uni-popup ref="popup" type="bottom">
			<view class="pay_type">
				<image @click="close" class="close" :src="'https://hdsq.aoorange.cn/attachment/xcx/icon/close1.png'" mode="aspectFill"></image>
				<view class="title">选择支付方式</view>
				<view class="paypir">
					<text>应付金额</text>
					<text>￥{{pay_money}}</text>
				</view>
				<view class="line3"  v-if="total_hdq > 0"  @click="setUseHdq()">
					<view :class="is_use_hdq == 1 ?'sel':'nosel'"></view>
					<text>使用互动券{{total_hdq}}抵扣金额￥{{total_hdq}}</text>
				</view>
				<view class="line2" >
					<text>余额支付 - 当前余额：{{user_money}}</text>
					<view :class="pay_type == 5 ?'sel':'nosel'" @click="selPayType(5)" v-if="Number(user_money) >= Number(pay_money)"></view>
					<text v-else>余额不足</text>
				</view>
				<view class="line2">
					<text>在线微信支付</text>
					<view :class="pay_type == 1 ?'sel':'nosel'" @click="selPayType(1)"></view>
				</view>
				<view class="pay_bot" @click="subPay()">确认支付</view>
			</view>
		</uni-popup>

	</view>
</template>

<script>
	import serve from '../../static/request.js';
	import storage from '../../static/appkey.js';
	import common from '../../static/jump.js';
	export default {
		data() {
			return {
				goodsList: [{}, {}],
				type: 0,
				id : 0,
				info : {
					order_status: -1,
					order_status_name: '',
					merchantName: '',
					receiver_name: '',
					receiver_mobile: '',
					receiver_region_name: '',
					receiver_address: '',
					product_money: 0,
					coin_money: 0,
					shipping_money: 0,
					order_money: 0,
					buyer_message: '',
					order_sn: '',
					create_time: '',
					pay_time: '',
					lobby_eat_time: '',
					consign_time: '',
					delivery_type: '',
					sign_time: '',
					cancel_time: '',
					shipping_time: '',
				},
				pay_type: 1,
				user_money: 0,
				user_integral: 0,
				is_use_hdq: 0,
				pay_money: 0,
				total_hdq: 0,
			}
		},
		methods: {
			getInfo() {
				var self = this;
				serve.request({
					url: '/member/order/view',
					data: {
						appkey: uni.getStorageSync('appkey'),
						'access-token': uni.getStorageSync('access-token'),
						id: self.id
					}
				}).then(res => {
					if(res.code != 200) {
						uni.showToast({
							title:res.message,
							icon: 'none',
							success() {
								setTimeout(function () {
									self.jump('store_order_list')
								}, 2000) 
							}
						});
					}
					uni.hideLoading();
					self.info = res.data;
					self.pay_money = self.info.pay_money;
				});
			},
			searchMemberInfo() {
				var self = this;
				serve.request({
					url: '/member/member/index',
					data: {
						appkey: uni.getStorageSync('appkey'),
						'access-token': uni.getStorageSync('access-token')
					}
				}).then(res => {
					self.user_money = res.data.user_money;
					self.user_integral = res.data.user_integral;
				});
			},
			setUseHdq(){
				this.is_use_hdq = this.is_use_hdq == 1 ? 0 : 1;
				let payMoney = Number(this.pay_money);
				if(this.is_use_hdq == 1) {
					payMoney = Number(payMoney) - Number(this.total_hdq);
				} else {
					payMoney = Number(payMoney) + Number(this.total_hdq);
				}
				this.pay_money = parseFloat(payMoney).toFixed(2);
				this.$forceUpdate();
			},
			operate(operateType, operateId) {
				var self = this;
				if(operateType == 'order_cancel') {
					uni.showModal({
					    title: '提示',
					    content: '您确定取消该订单吗',
					    success: function (res) {
					        if (res.confirm) {
					            serve.request({
									url: '/member/order/cancel',
									data:{
										appkey: uni.getStorageSync('appkey'),
										'access-token': uni.getStorageSync('access-token'),
										id:operateId,
									}
								}).then(res => {
									uni.showToast({
										title:res.message,
										icon: 'none'
									});
									if(res.code == 200) {
										self.getInfo();
										self.$forceUpdate();
									}
								});
					        } 
					    }
					});
					return ;
				} else if(operateType == 'order_pay'){
					self.is_use_hdq = 0;
					self.pay_money = self.info.pay_money;
					self.use_point = self.info.use_point;
					self.pay_id = operateId;
					if(Number(self.user_integral) - Number(self.use_point) >= 0) {
						self.total_hdq = self.use_point;
					} else {
						self.total_hdq = self.user_integral;
					}
					self.$refs.popup.open();
					return ;
				} else if(operateType == 'tip_delivery'){
					serve.request({
						url: '/member/order/tip-delivery',
						data:{
							appkey: uni.getStorageSync('appkey'),
							'access-token': uni.getStorageSync('access-token'),
							id:operateId,
						}
					}).then(res => {
						uni.showToast({
							title:res.message,
							icon: 'none'
						});
						if(res.code == 200) {
							self.getInfo();
							self.$forceUpdate();
						} 
					});
				} else if(operateType == 'task_delivery'){
					uni.showModal({
					    title: '提示',
					    content: '您确定该订单已确定收货吗',
					    success: function (res) {
					        if (res.confirm) {
					            serve
					            	.request({
					            		url: '/member/order/take-delivery',
					            		data:{
											appkey: uni.getStorageSync('appkey'),
											'access-token': uni.getStorageSync('access-token'),
											id:operateId,
										}
					            	})
					            	.then(res => {
										uni.showToast({
											title:res.message,
											icon: 'none'
										});
					            		if(res.code == 200) {
											self.getInfo();
											self.$forceUpdate();
					            		} 
					            	});
					        } 
					    }
					});
					return ;
				} else if(operateType == 'take_code'){
					uni.navigateTo({
					    url: '/pages/merchant/takeCode?orderId='+operateId
					});
					return ;
				} else if(operateType == 'evaluate_order'){
					common.jump('evaluate_store', self.info.merchantId+'&orderId='+operateId);
				} else if(operateType == 'call_merchant') {
					self.callPhone(self.info.merchantMobile);
					return ;
				} else if(operateType == 'call_delivery') {
					self.callPhone(self.info.delivery_no);
					return ;
				}
			},
			selPayType(type) {
				this.pay_type = type;
			},
			close() {
				this.$refs.popup.close();
			},
			subPay() {
				var self = this;
				if(self.pay_type == 5) {
					uni.showLoading({
						title: '支付结果查询中···',
						mask: true
					});
					serve.request({
						url: '/order/pay/money',
						data:{
							appkey: uni.getStorageSync('appkey'),
							'access-token': uni.getStorageSync('access-token'),
							data_id:self.pay_id,
							is_use_hdq: self.is_use_hdq
						}
					}).then(res => {
						uni.hideLoading();
						if(res.code != 200) {
							uni.showToast({
								title:res.message,
								icon: 'none'
							});
							return ;
						}
						self.getInfo();
					});
					return ;
				}
				serve.request({
					url: '/order/pay/wechat',
					data:{
						appkey: uni.getStorageSync('appkey'),
						'access-token': uni.getStorageSync('access-token'),
						data_id:self.pay_id,
						data_type: 'order_pay',
						is_use_hdq: self.is_use_hdq
					}
				}).then(res => {
					if(res.code != 200) {
						uni.showToast({
							title:res.message,
							icon: 'none'
						});
						return ;
					}
					let payId = res.data.payId;
					let payConfig = res.data.payConfig;
					uni.requestPayment({
						provider: 'wxpay',
						timeStamp: payConfig.timeStamp,
						nonceStr: payConfig.nonceStr,
						package: payConfig.package,
						signType: payConfig.signType,
						paySign: payConfig.paySign,
						success: function (res) {
							// self.paySuccess(operateId, 'wechat');
							self.queryPayResult(payId);
							return ;
						},
						fail: function (err) {
							self.queryPayResult(payId);
							uni.showToast({
								title: '放弃支付',
								icon: 'none'
							});
							return ;
						}
					})
				});
				return ;
			},
			queryPayResult(payId) {
				uni.showLoading({
					title: '支付结果查询中···',
					mask: true
				});
				var self = this;
				setTimeout(function() {
					serve.request({
						url: '/payservice/query-pay',
						data:{
							appkey: uni.getStorageSync('appkey'),
							payId: payId,
						}
					}).then(res => {
						if(res.data.payStatus == 1) {
							self.getInfo();
							self.$refs.popup.close();
						} else {
							self.queryPayResult(payId);
						}
					});
				}, 1500);
			},
			showMap() {
				uni.openLocation({
				    latitude: Number(this.info.receiver_latitude),
				    longitude: Number(this.info.receiver_longitude),
				    success: function (res) {
				        console.log(res);
				    }
				});
			},
			callPhone(mobile) {
				uni.makePhoneCall({
				    phoneNumber: mobile
				});
			},
			jump(jump_type, jump_link){
				common.jump(jump_type, jump_link);
			}
		},
		onLoad(options) {
			this.id=options.id || 0;
			if(this.id == 0) {
				uni.showToast({
					title: "错误的访问方式",
					icon: 'none',
					success() {
						setTimeout(function () {
							common.jump('return_one')
						}, 2000) 
					}
				});
			}
		},
		onShow() {
			uni.showLoading({
				title: '加载中···',
				mask: true
			});
			this.getInfo();
			this.searchMemberInfo();
		},
		onPullDownRefresh() {
			uni.showLoading({
				title: '加载中···',
				mask: true
			});
			this.getInfo();
		}
	}
</script>

<style scoped lang="scss">
	@media only screen and (device-width: 375px) and (device-height: 812px) and (-webkit-device-pixel-ratio: 3) {
		    .orderDetails {
		        padding-bottom: 124upx !important;
				.bot{
					 padding-bottom: 12px !important;
				}
		    }
		}
	.orderDetails {
		padding-bottom: 100upx;

		.top {
			position: relative;
			border-bottom: 16upx rgba(251, 251, 253, 1) solid;

			.state {
				position: relative;
				box-sizing: border-box;
				// padding: 58upx 48upx;
				width: 100%;
				height: 250upx;
				// background: #F5F5FE;
				display: flex;
				justify-content: space-between;
				align-items: center;
				position: relative;
				font-size: 48upx;
				font-family: YouSheBiaoTiHei;
				font-weight: bold;
				color: #333333;
				image {
					position: absolute;
					width: 100%;
					height: 250upx;
					z-index: 0;
				}
				.right{
					position: relative;
					z-index: 10;
					display: flex;
					flex-direction: column;
					padding-left: 30upx;
					// width: 280upx;
					.time {
						// position: absolute;
						// bottom: 18upx;
						color: #333333;
						font-size: 20upx;
						font-style:normal;
						font-weight: 400;
						margin-top: 14upx;
					}
				}

				
			}
            .line1{
				box-sizing: border-box;
				display: flex;
				padding: 0upx 30upx;
				height: 130upx;
				align-items: center;
				justify-content: space-between;
				border-bottom: 1upx rgba(53,63,110,0.06) solid;
				.left{
					display: flex;
					align-items: center;
					font-size: 28upx;
					color: #666666;
					image{
						width:40upx;
						height:29upx;
						margin-right: 13upx;
					}
				}
				.right{
					color: #EB5938;
					font-size: 28upx;
					font-weight: bold;
				}
			}
			.address {
				display: flex;
				position: relative;
				padding: 53upx 70upx;
				font-size: 28upx;
				align-items: center;
				color: #666666;

				image {
					position: absolute;
					left: 30upx;
					width: 31upx;
					height: 37upx;
					top: 120upx;
				}

				.txt {
					display: flex;
					justify-content: space-between;
					padding-bottom: 28upx;
				}

				.jt {
					position: absolute;
					right: 30upx;
					top: 105upx;
					content: '';
					border: 1upx solid rgba(57, 72, 107, 0.5);
					border-width: 0 3upx 3upx 0;
					padding: 6upx;
					transform: rotate(-45deg);
					-webkit-transform: rotate(-45deg);
				}
			}

			.img {
				position: absolute;
				bottom: 0;
				height: 10upx;
				width: 100%;
			}
		}

		.goods {
			box-sizing: border-box;
			display: flex;
			align-items: center;
			padding: 40upx 30upx;
			border-bottom: 1upx rgba(235, 236, 240, 1) solid;

			image {
				width: 170upx;
				height: 170upx;
				margin-right: 20upx;
			}

			.right {
				position: relative;
				width: 503upx;
				display: flex;
				// justify-content: center;
				flex-direction: column;
				font-size: 32upx;

				.operate {
					position: absolute;
					right: 0upx;
					bottom: 0upx;
					width: 130upx;
					height: 48upx;
					border: 1px solid #999999;
					border-radius: 24upx;
					text-align: center;
					font-size: 24upx;
					line-height: 48upx;
					font-weight: bold;
					color: #999999;
				}

				.size {
					display: flex;
					justify-content: space-between;
					padding: 20upx 0upx;
					color: #999999;
					font-size: 24upx;
					align-items: center;
				}

				text:first-child {
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}

				.num {
					font-size: 28upx;
					font-weight: bold;
					color: #FF5F60;
				}
				.num1{
					font-size:20upx;
					font-weight:400;
					text-decoration:line-through;
					color: #CCCCCC;
				}
			}
		}

		.line {
			box-sizing: border-box;
			padding: 37upx 30upx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 28upx;
			// font-weight: bold;
			color: #666666;
			border-bottom: 1upx rgba(235, 236, 240, 1) solid;

			text:last-child {
				color: #FF5F60;
				font-size: 32upx;
			}

			;
		}

		.pic {
			box-sizing: border-box;
			padding: 37upx 30upx;
			display: flex;
			justify-content: flex-end;
			align-items: flex-end;
			font-size: 28upx;
			color: #999999;
			border-bottom: 1upx rgba(235, 236, 240, 1) solid;

			text:last-child {
				color: #FF5F60;
				font-size: 32upx;
				font-weight: bold;
			}

			;
		}

		.common {
			box-sizing: border-box;
			padding: 37upx 30upx;
			display: flex;
			justify-content: space-between;
			flex-wrap: wrap;
			align-items: flex-start;
			// font-weight: bold;
			font-size: 28upx;
			color: #666666;
			border-bottom: 16upx rgba(251, 251, 253, 1) solid;

			text:last-child {
				max-width: 540upx;
			}

			;
		}

		.info {
			display: flex;
			flex-direction: column;
			padding: 47upx 30upx;
			font-size: 28upx;
			color: #666666;

			text:first-child {
				font-weight: bold;
				margin-top: 0upx;
				color: #333333;
			}

			text {
				font-weight: 400;
				margin-top: 20upx;
			}
		}

		.bot {
			position: fixed;
			width: 100%;
			height: 100upx;
			bottom: 0upx;
			border-top: 1upx #EBECF0 solid;
            padding-bottom: env(safe-area-inset-bottom);
			.txt1 {
				box-shadow: 0px 1upx 0px 0px rgba(235, 236, 240, 1);
				width: 100%;
				height: 100%;
				background: #FFBF0A;
				color: #FFFFFF;
				font-size: 28upx;
				text-align: center;
				line-height: 100upx;
				display: flex;
				text {
					background: #FFBF0A;
					color: #FFFFFF;
					height: 100upx;
					line-height: 100upx;
					width: 100%;
				}
			}

			.txt2 {
				box-shadow: 0px 1upx 0px 0px rgba(235, 236, 240, 1);
				display: flex;
				width: 100%;
				height: 100%;

				text {
					height: 100upx;
					line-height: 100upx;
					text-align: center;
					width: 50%;
					font-size: 28upx;
					background: #FFFFFF;
					color: #666666;
				}

				text:last-child {
					background: #FFBF0A;
					color: #FFFFFF;
				}
			}
		}
		.pay_type{
			position: relative;
			width: 100%;
			background: #FFFFFF;
			padding-bottom: 140upx;
			.close{
				position: absolute;
				width:23upx;
				height:23upx;
				right: 40upx;
				top: 45upx;
			}
			.title{
				width: 100%;
				text-align: center;
				height: 100upx;
				line-height: 100upx;
				font-size: 36upx;
				border-bottom: 1upx rgba(251, 251, 253, 1) solid;
			}
			.paypir{
				display: flex;
				justify-content: center;
				flex-direction: column;
				align-items: center;
				padding: 50upx 0upx 20upx;
				font-size: 28upx;
				text:last-child{
					color: #FF5F60;
					font-size: 40upx;
					font-weight: bold;
					margin-top: 10upx;
				}
			}
			.line2{
				width: 100%;
				box-sizing: border-box;
				padding: 36upx;
				display: flex;
				justify-content: space-between;
				font-size: 28upx;
				color: #333333;
				border-bottom: 1upx rgba(251, 251, 253, 1) solid;
				.txt{
					color: #999999;
				}
				.sel {
					background-image: url(https://hdsq.aoorange.cn/attachment/xcx/icon/xuanzhong.png);
					background-repeat: no-repeat;
					width: 40upx;
					height: 40upx;
					background-size: 100%;
				}
				
				.nosel {
					width: 36upx;
					height: 36upx;
					border: 1upx solid rgba(60, 72, 104, 1);
					border-radius: 50%;
				}
			}
			.line3{
				width: 100%;
				box-sizing: border-box;
				padding: 36upx;
				display: flex;
				font-size: 28upx;
				color: #333333;
				border-bottom: 1upx rgba(251, 251, 253, 1) solid;
				.txt{
					color: #999999;
					padding-left: 20upx;
				}
				.sel {
					background-image: url(https://hdsq.aoorange.cn/attachment/xcx/icon/xuanzhong.png);
					background-repeat: no-repeat;
					width: 40upx;
					height: 40upx;
					background-size: 100%;
				}
				
				.nosel {
					width: 36upx;
					height: 36upx;
					border: 1upx solid rgba(60, 72, 104, 1);
					border-radius: 50%;
				}
			}
			.pay_bot {
			  width: 100%;
			  position: fixed;
			  left: 0;
			  bottom: 0;
			  height: 100upx;
			  background: #FFBF0A;
			  color: #fff;
			  font-size: 32upx;
			  line-height: 100upx;
			  text-align: center;
			  border-radius: 0;
			  &::after{
				  border: none;
			  }
			}
		}
	}
</style>
